package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.DateJsonValueProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;

import cn.jekun.web.shop.bean.ShopDelivery;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpDelivery;
import cn.jekun.component.core.bean.gen.ErpPublishTask;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.ErpShopImage;
import cn.jekun.component.core.bean.gen.ErpShoptype;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.bean.gen.RsUserShop;
import cn.jekun.component.core.service.gen.ErpDeliveryService;
import cn.jekun.component.core.service.gen.ErpPublishTaskService;
import cn.jekun.component.core.service.gen.ErpShopService;
import cn.jekun.component.core.service.gen.ErpShoptypeService;
import cn.jekun.web.shop.bean.Shop;
import cn.jekun.web.shop.bean.Terminal;
import cn.jekun.web.shop.bean.UserShop;
import cn.jekun.web.shop.service.LoginService;
import cn.jekun.web.shop.service.ShopService;
import cn.jekun.web.shop.service.ShopUserService;

/**
 * 商户
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class ShopAction extends ShopBaseAction {
	private static final String SHOP_MY_INFO_MANAGER="SHOP_MY_INFO_MANAGER";//基本信息
	private static final String AUTH_SHOP_TERNIMAL_MONITOR="SHOP_TERNIMAL_MONITOR";//设备监控
	private static final String SHOP_STAFF_MANAGER = "SHOP_STAFF _MANAGER";
	private String shopId;
	private ErpShop shop;
	private String terminalId;
	private ErpShoptype erpShoptype;
	private String parentTypeId;
	private String childTypeId;
	private RsUser rsUser;
	
	private List<ErpShoptype> ErpShoptypeList = new ArrayList<ErpShoptype>();
	private List<UserShop> userShopList = null;
	
	@Resource(name=ShopService.BEAN_NAME)
	ShopService shopService;
	
	@Resource(name=ErpShoptypeService.BEAN_NAME)
	ErpShoptypeService erShoptypeService;
	
	@Resource(name="cn.jekun.component.core.service.ShopService")
	cn.jekun.component.core.service.ShopService tempShopService;

	@Resource(name=LoginService.BEAN_NAME)
	LoginService loginService;

	@Resource(name=ErpShoptypeService.BEAN_NAME)
	ErpShoptypeService erpShoptypeService;


	@Resource(name=ErpShopService.BEAN_NAME)
	ErpShopService erpShopService;
	
	@Resource(name = ErpPublishTaskService.BEAN_NAME)
	ErpPublishTaskService erpPublishTaskService;
	
	@Resource(name = ShopUserService.BEAN_NAME)
	ShopUserService shopUserService;
	
	@Resource(name = ErpDeliveryService.BEAN_NAME)
	ErpDeliveryService erpDeliveryService;
	
	
	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public ErpShop getShop() {
		return shop;
	}

	public void setShop(ErpShop shop) {
		this.shop = shop;
	}

	public String getTerminalId() {
		return terminalId;
	}

	public void setTerminalId(String terminalId) {
		this.terminalId = terminalId;
	}

	public ErpShoptype getErpShoptype() {
		return erpShoptype;
	}

	public void setErpShoptype(ErpShoptype erpShoptype) {
		this.erpShoptype = erpShoptype;
	}

	public String getParentTypeId() {
		return parentTypeId;
	}

	public void setParentTypeId(String parentTypeId) {
		this.parentTypeId = parentTypeId;
	}

	public String getChildTypeId() {
		return childTypeId;
	}

	public void setChildTypeId(String childTypeId) {
		this.childTypeId = childTypeId;
	}
	
	public RsUser getRsUser() {
		return rsUser;
	}

	public void setRsUser(RsUser rsUser) {
		this.rsUser = rsUser;
	}

	/**
	 * 查询shopId的商户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryShop(){
		//判断是否有操作权限
//		this.checkRight(AUTH_SHOP_MY_INFO);
//		if(StringUtils.isBlank(this.getSessionUser().getShopId())){
//			this.saveActionError("商户编码不能为空");
//			return "/shop/shopdetail.jsp";
//		}
//		formMap.put(Shop.COL_SHOP_ID, this.getSessionUser().getShopId());
//		SqlUtis.prepareSql(formMap
//				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
//				);
		
//		return "/shop/shopdetail.jsp";
		
		
		
		
		JSONObject jsonObject=new JSONObject();
		
		try{
		String shopId= this.getSessionUser().getShopId();
		Shop tempshop = (Shop) this.shopService.one(shopId);
		System.out.println("配送时间："+tempshop.getShipTime());
		int doneNum = this.shopService.setDomainNums(shopId);//已设置过的次数
		int todoNum = 0;//剩下设置的次数
		if(doneNum<3){
			todoNum = (3-doneNum);
		}
		if(tempshop!=null){
			jsonObject.put("doneNum", doneNum);
			jsonObject.put("todoNum", todoNum);
			jsonObject.put(Shop.COL_SHOP_ID, tempshop.getShopId()==null?"":tempshop.getShopId());
			
			jsonObject.put(Shop.COL_LOGO_URL, tempshop.getLogoUrl()==null?"":tempshop.getLogoUrl());
			
			jsonObject.put(Shop.COL_URL, tempshop.getUrl()==null?"":tempshop.getUrl());
			
			jsonObject.put(Shop.COL_SHOP_TYPE_ID, tempshop.getShopTypeId()==null?"":tempshop.getShopTypeId());
			jsonObject.put("SHOP_TYPE_NAME", tempshop.getShopTypeName()==null?"":tempshop.getShopTypeName());
			
			jsonObject.put(Shop.COL_COM_NAME, tempshop.getComName()==null?"":tempshop.getComName());

			jsonObject.put(Shop.COL_SHOP_ID, tempshop.getShopId()==null?"":tempshop.getShopId());
			jsonObject.put(Shop.COL_SHOP_NAME, tempshop.getShopName()==null?"":tempshop.getShopName());
			jsonObject.put(Shop.COL_INTRODUCE, tempshop.getIntroduce()==null?"":tempshop.getIntroduce());
			
			jsonObject.put(Shop.COL_AGENT_ID, tempshop.getAgentId()==null?"":tempshop.getAgentId());
			jsonObject.put("AGENT_NAME", tempshop.getAgentName()==null?"":tempshop.getAgentName());
			
			jsonObject.put(Shop.COL_PROVINCE_ID, tempshop.getProvinceId()==null?"":tempshop.getProvinceId());
			jsonObject.put("PROVINCE_NAME", tempshop.getProvinceName()==null?"":tempshop.getProvinceName());
			
			jsonObject.put(Shop.COL_CITY_ID, tempshop.getCityId()==null?"":tempshop.getCityId());
			jsonObject.put("CITY_NAME", tempshop.getCityName()==null?"":tempshop.getCityName());
			
			jsonObject.put(Shop.COL_REGION_ID, tempshop.getRegionId()==null?"":tempshop.getRegionId());
			jsonObject.put("REGION_NAME", tempshop.getRegionName()==null?"":tempshop.getRegionName());
			
			jsonObject.put(Shop.COL_STREET, tempshop.getStreet()==null?"":tempshop.getStreet());
			
			jsonObject.put(Shop.COL_ADDRESS, tempshop.getAddress()==null?"":tempshop.getAddress());
			
			jsonObject.put(Shop.COL_CONTACT, tempshop.getContact()==null?"":tempshop.getContact());
			
			jsonObject.put(Shop.COL_MOBILE, tempshop.getMobile()==null?"":tempshop.getMobile());
			
			jsonObject.put(Shop.COL_MSG_MOBILE, tempshop.getMsgMobile()==null?"":tempshop.getMsgMobile());
			
			jsonObject.put(Shop.COL_PHONE, tempshop.getPhone()==null?"":tempshop.getPhone());
			
			jsonObject.put(Shop.COL_BUSINESS_HOURS, tempshop.getBusinessHours()==null?"":tempshop.getBusinessHours());
			
			jsonObject.put(Shop.COL_BUSINESS_STATUS, tempshop.getBusinessStatus()==null?"":tempshop.getBusinessStatus());
			
			jsonObject.put(Shop.COL_BUSINESS_STATUS_TYPE, tempshop.getBusinessStatusType()==null?"":tempshop.getBusinessStatusType());
			
			jsonObject.put(Shop.COL_MIN_AMOUNT, tempshop.getMinAmount()==null?"":tempshop.getMinAmount());
			
			jsonObject.put(Shop.COL_RANGE, tempshop.getRange()==null?"":tempshop.getRange());
			
			jsonObject.put(Shop.COL_DELIVERY_AMOUNT, tempshop.getDeliveryAmount()==null?"0":tempshop.getDeliveryAmount());
			
			jsonObject.put(Shop.COL_SHIP_TIME, tempshop.getShipTime()==null?"":tempshop.getShipTime());
			
			jsonObject.put(Shop.COL_LATITUDE, tempshop.getLatitude()==null?"":tempshop.getLatitude());
			
			jsonObject.put(Shop.COL_LONGITUDE, tempshop.getLongitude()==null?"":tempshop.getLongitude());
			
			jsonObject.put("MCC", tempshop.getMcc()==null?"":tempshop.getMcc());
			
			jsonObject.put(Shop.COL_MEMO, tempshop.getMemo()==null?"":tempshop.getMemo());
			
			jsonObject.put(Shop.COL_MERCHANT_NAME, tempshop.getMerchantName()==null?"":tempshop.getMerchantName());
			
			jsonObject.put(Shop.COL_MERCHANT_NO, tempshop.getMerchantNo()==null?"":tempshop.getMerchantNo());
			
		}
		
		//配送方式
		List<ShopDelivery> erpDeliverys=this.shopService.getDeliveryList(shopId);
		JSONArray erpDeliveryArray=new JSONArray();

		if(erpDeliverys !=null){
					
					
					
			for (int i=0;i<erpDeliverys.size();i++) {
						
				Map	map = new HashMap();
						
				map.put(ErpDelivery.COL_DELIVERY_ID, erpDeliverys.get(i).getDeliveryId());
						
				map.put(ErpDelivery.COL_DELIVERY_NAME,erpDeliverys.get(i).getDeliveryName());
						
				erpDeliveryArray.add(map);

			}
		}

		jsonObject.put("erpDeliverys",erpDeliveryArray);
		
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
	
			
		return this.writeJson(jsonObject);
		
		
	}
//	/**
//	 * 根据shopId得到下面所有的终端信息（设备监控）
//	 * @return
//	 */
//	@SuppressWarnings("unchecked")
//	public String queryTerminalByShopId(){
////		//判断是否有操作权限
////		this.checkRight(AUTH_SHOP_TERNIMAL_MONITOR);
////		formMap.put(Shop.COL_SHOP_ID, this.getSessionUser().getShopId());
////		formMap.put(RsTerminal.COL_TERMINAL_ID, terminalId);
////		SqlUtis.prepareSql(formMap
////				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
////				SqlUtis.getSQL(formMap,"a."+RsTerminal.COL_TERMINAL_ID,Types.VARCHAR,SqlUtis.LIKE)
////				);
////		this.resultList = this.shopTerminalService.list(formMap, page);
////		return "/shop/shopTerminal.jsp";
//		
//		JSONObject jsonObject=new JSONObject();
//
//		try {
//			String shopId=this.getSessionUser().getShopId();
//			
//			this.resultList = this.shopTerminalService.queryTerminallList(shopId,terminalId, page);
//
//			JSONArray jsonArray = new JSONArray();
//
//			HashMap<String, Object> hashMap;
//
//			for(int i=0;i<this.resultList.size();i++){
//
//				hashMap=new HashMap<String, Object>();
//				
//				Terminal tempTerminal =(Terminal) resultList.get(i);
//
//				hashMap.put(Terminal.TERMINAL_ID, tempTerminal.getTerminalId()==null?"":tempTerminal.getTerminalId());
//
//				hashMap.put(Terminal.SHOP_NAME, tempTerminal.getShopName()==null?"":tempTerminal.getShopName());
//
//				hashMap.put(Terminal.VERNDOR_NAME, tempTerminal.getVendorName()==null?"":tempTerminal.getVendorName());
//
//				hashMap.put(Terminal.CURRENT_VERSION, tempTerminal.getVersion()==null?"":tempTerminal.getVersion());
//
//				hashMap.put("available", tempTerminal.getAvailable()==null?"":tempTerminal.getAvailable());
//
//				hashMap.put(Terminal.TOTAL, tempTerminal.getTotal()==null?"":tempTerminal.getTotal());
//
//
//				jsonArray.add(hashMap);
//
//			}
//
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
//
//			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
//
//			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
//
//			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
//
//			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
//
//			jsonObject.put("terminalListData", jsonArray);
//
//			
//		}catch (BusinessException e) {
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		
//		return this.writeJson(jsonObject);
//		
//	}
	/**
	 * 根据shopId加载商户数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String loadTerminalByShopId(){
		if(StringUtils.isBlank(this.getSessionUser().getShopId())){
			this.saveActionError("商户编码不能为空");
			return "/shop/shopInfo.jsp";
		}
		formMap.put(Shop.COL_SHOP_ID, this.getSessionUser().getShopId());
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		//商家类型-父类List
		Map typeMap = new HashMap();
		SqlUtis.prepareSql(typeMap
				, "AND a.PARENT_ID IS NULL"
				);
		List<ErpShoptype> erpShoptypes = this.erpShoptypeService.list(typeMap);
		request.setAttribute("erpShoptypes", erpShoptypes);
		shop = (ErpShop) this.shopService.one(formMap);
		//查询商家类型
		String shopTypeId = shop.getShopTypeId();
		erpShoptype = new ErpShoptype();
		if(shopTypeId!=null){
			typeMap = new HashMap();
			typeMap.put(ErpShoptype.COL_SHOP_TYPE_ID, shopTypeId);
			SqlUtis.prepareSql(typeMap
					, SqlUtis.getSQL(typeMap,"a."+ErpShoptype.COL_SHOP_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
					);
			erpShoptype = erpShoptypeService.one(typeMap);
		}
		return "/shop/shopInfo.jsp";
	}

	/**
	 * 保存商户数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String saveTerminalByShopId(){
		//判断商家类型
		if(childTypeId==null){
			shop.setShopTypeId(parentTypeId);
		}else{
			shop.setShopTypeId(childTypeId);
		}
		formMap.put("erpShop", shop);
		formMap.put(Shop.COL_SHOP_ID, this.getSessionUser().getShopId());
		formMap.put("userName", this.getSessionUser().getUserName());
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Shop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		boolean exists = this.erpShopService.exists(formMap);
		if (!exists) {
			this.saveActionError("商户不存在");
			return "!/shop/Shop/queryShop.do";
		}
		int returnId= this.tempShopService.update(formMap);
		System.out.println(returnId);
		// 记录操作日志
		this.logService.addShopAuditLog(SHOP_MY_INFO_MANAGER, "修改商户信息");
		this.getSessionUser().setUserShops(loginService.getUserShops(formMap));
		this.getSessionUser().setShopName(shop.getShopName());

		//更新发布任务
		try {
			Map publishTaskMap = new HashMap();
			ErpPublishTask publishtask = new ErpPublishTask();
			publishtask.setTaskStatus("11");
			publishTaskMap.put(ErpPublishTask.COL_TASK_ID, this.getSessionUser().getShopId());
			publishTaskMap.put(ErpPublishTask.COL_TASK_TYPE_ID, "04");
			publishTaskMap.put("erpPublishTask",publishtask);
			SqlUtis.prepareSql(publishTaskMap,
					SqlUtis.getSQL(publishTaskMap,"a."+ErpPublishTask.COL_TASK_ID,Types.VARCHAR,SqlUtis.EQ),
					SqlUtis.getSQL(publishTaskMap,"a."+ErpPublishTask.COL_TASK_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
					);
			this.erpPublishTaskService.update(publishTaskMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "!/shop/Shop/queryShop.do";
	}

	/**
	 * 通过id,查询二级分类
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String findTypeForId(){
		Map mapInfo = new HashMap();
		mapInfo.put(ErpShoptype.COL_PARENT_ID, request.getParameter("parentId"));
		SqlUtis.prepareSql(mapInfo
				, SqlUtis.getSQL(mapInfo,"a."+ErpShoptype.COL_PARENT_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		List<ErpShoptype> erpShoptypes = this.erpShoptypeService.list(mapInfo);
		JSONArray jsonOb= JSONArray.fromObject(erpShoptypes);
		return this.writeJson(jsonOb);
	}




	/**
	 * 商家类型列表
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String getErpShopTypeList(){

		JSONObject jsonObject=new JSONObject();
		
		this.ErpShoptypeList = this.erShoptypeService.list(null);
		
		JSONArray jsonArray = new JSONArray();
		
		HashMap<String, Object> hashMap;
		
		List<String> shopTypeName=new ArrayList<String>();
		
		for(int i=0;i<this.ErpShoptypeList.size();i++){
			
				if(!shopTypeName.contains(ErpShoptypeList.get(i).getShopTypeName()) && !ErpShoptypeList.get(i).getShopTypeName().equals("更多")){
					
					hashMap=new HashMap<String, Object>();
					
					hashMap.put(ErpShoptype.COL_SHOP_TYPE_ID, ErpShoptypeList.get(i).getShopTypeId());
					
					hashMap.put(ErpShoptype.COL_SHOP_TYPE_NAME, ErpShoptypeList.get(i).getShopTypeName());
					
					hashMap.put(ErpShoptype.COL_PARENT_ID,ErpShoptypeList.get(i).getParentId());
					
					jsonArray.add(hashMap);
					
//					shopTypeName.add(ErpShoptypeList.get(i).getShopTypeName());
					
				}
			
		}
		
//		hashMap=new HashMap<String, Object>();
//		
//		hashMap.put(ErpShoptype.COL_SHOP_TYPE_ID, "");
//		
//		hashMap.put(ErpShoptype.COL_SHOP_TYPE_NAME, "更多");
//		
//		hashMap.put(ErpShoptype.COL_PARENT_ID,"");
//		
//		jsonArray.add(hashMap);
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

		jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
		
		jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
		
		jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
		
		jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
		
		jsonObject.put("shopTypeListData", jsonArray);
		
		return this.writeJson(jsonObject);
	}
	
	
	
	/**
	 * 更新商户资料
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String editShop(){
		
		JSONObject jsonObject=new JSONObject();
		try {
			String shopId=this.getSessionUser().getShopId();

			String shopName=request.getParameter("shopName");
			String introduce=request.getParameter("introduce");
			String comName=request.getParameter("comName");
			String shopTypeId=request.getParameter("shopTypeId");
			String agentId=request.getParameter("agentId");
			String provinceId=request.getParameter("provinceId");
			String cityId=request.getParameter("cityId");
			String regionId=request.getParameter("regionId");
			String street=request.getParameter("street");
			String address=request.getParameter("address");
			String contact=request.getParameter("contact");
			String businessHours=request.getParameter("businessHours");
			String minAmount=request.getParameter("minAmount");
			String range=request.getParameter("range");
			String shipTime=request.getParameter("shipTime");
			String phone=request.getParameter("phone");
			String mobile=request.getParameter("mobile");
			String logoUrl=request.getParameter("logoUrl");
			String longitude=request.getParameter("longitude");
			String latitude=request.getParameter("latitude");
			String mcc=request.getParameter("mcc");
			String memo=request.getParameter("memo");
			String businessStatusType=request.getParameter("businessStatusType");
			String businessStatus=request.getParameter("businessStatus");
			String msgMobile = request.getParameter("msgMobile");
			String deliveryAmount = request.getParameter("deliveryAmount");
			String shopDeliveryId = request.getParameter("shopDeliveryId");
			
			this.shopService.updateShop(shopId,shopName,introduce,comName,shopTypeId,agentId,provinceId,cityId,regionId,street,
					address,contact,businessHours,minAmount,range,shipTime,phone,mobile,logoUrl,longitude,
					latitude,mcc,memo,businessStatusType,businessStatus,msgMobile,deliveryAmount,shopDeliveryId);
			
			
			
			
			
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"修改商户资料成功:"+shopId);

			this.logService.addShopAuditLog(SHOP_MY_INFO_MANAGER, "修改商户资料成功，修改人:"+this.getSessionUser().getUserName());//记录操作日志
			//this.saveActionMessage("修改商户资料成功，商户代码:"+erpShop.getShopId());
//			System.err.println("一致性");
			System.out.println("jsonObject = " + jsonObject);
			
		} catch (BusinessException e) {
			System.out.println(e);
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}


		return this.writeJson(jsonObject);

		//return "!/admin/Shops/getShopsById.do?shopId="+erpShop.getShopId();
	}
	/**
	 * 获取店员用户基本信息
	 * @return
	 */
	public String getRsUsersList(){
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		try{
		String shopId = this.getSessionUser().getShopId();
		String userName = request.getParameter("username");
		String realName = request.getParameter("realName");
		String mobile = request.getParameter("mobile");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		String staffId = request.getParameter("staffId");
		String userId =  request.getParameter("userId");
//		userShopList = shopUserService.getUserShopList(realName,userName, mobile, beginDate, endDate, staffId, userId, shopId, page);
		userShopList = shopUserService.getUserShopList(realName, userName, mobile, beginDate, endDate, staffId, userId, shopId, page);
		jsonObject.put("userShopList", JSONArray.fromObject(userShopList,jsonConfig));
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
		jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
		jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
		jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
		}catch(BusinessException e){
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 添加店员
	 * @return
	 */
	public String addUsersStaff(){
	    JSONObject jsonObject = new JSONObject();
		try{
		String tempShopId = this.getSessionUser().getShopId();
		String tempUserId = this.request.getParameter("userId");
		String tempMemo = this.request.getParameter("memo");
	    RsUserShop userShop=null;
	    if(this.shopUserService.exitsByUserId(tempUserId)){
	    	jsonObject.put(ErrorMsg.KEY_CODE,ErrorMsg.CALL_FAIL);
	    	jsonObject.put(ErrorMsg.KEY_MESSAGE,"该用户已经是别家店员了。");
	        return this.writeJson(jsonObject);
	    }
	    boolean flage = this.shopUserService.exitsByShopIdUserId(tempUserId, tempShopId);
	    if(flage){
	    	jsonObject.put(ErrorMsg.KEY_CODE,ErrorMsg.CALL_FAIL);
	    	jsonObject.put(ErrorMsg.KEY_MESSAGE,"该用户已经是你的店员了。");
	        return this.writeJson(jsonObject);
	    }
	    userShop = new RsUserShop();
	    userShop.setMemo(tempMemo);
	    userShop.setUserId(tempUserId);
	    userShop.setShopId(tempShopId);
	    this.shopUserService.addRsUserShop(userShop);
	    jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"添加店员成功");
		this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "添加店员成功，店员代码:"+userShop.getUserId());//记录操作日志
	    }catch(BusinessException e){
	    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 查询所有的用户
	 * @return
	 */
	public String queryAllUser(){
		JSONObject jsonObject = null;
		JSONArray jsonArray = null;
		List<RsUser> listUser = null;
		Map hashMap=null;
		try{
			jsonObject = new JSONObject();
			jsonArray = new JSONArray();
			String tempUserId = this.request.getParameter("user_id");
			String tempUserName = this.request.getParameter("user_name");
			listUser = this.shopService.queryAllUser(tempUserId,tempUserName,page);
			for(RsUser user : listUser){
				hashMap = new HashMap();
				hashMap.put(RsUser.COL_USER_NAME, user.getUserName()==null?"":user.getUserName());
				hashMap.put(RsUser.COL_MOBILE, user.getMobile()==null?"":user.getMobile());
				hashMap.put(RsUser.COL_EMAIL, user.getEmail()==null?"":user.getEmail());
			    hashMap.put(RsUser.COL_USER_ID,user.getUserId());
			    hashMap.put(RsUser.COL_REAL_NAME,user.getRealName()==null?"":user.getRealName());
			    jsonArray.add(hashMap);
			}
			jsonObject.put("userListData", jsonArray);
			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
            jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
            jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
		}catch(BusinessException e){
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"添加店员失败");
		}
		return this.writeJson(jsonObject);
	}
	
	
	/**
	 * 更新营业状态
	 * @return
	 */
	public String updateBusinessStatus(){
		JSONObject jsonObject=new JSONObject();
		try {
			String shopId=this.getSessionUser().getShopId();

			String businessStatus=request.getParameter("businessStatus");
			
			int ret = this.shopService.updateBusinessStatus(shopId,businessStatus);
			if(ret>0){
				String status=null;
				if(businessStatus.equals("1")){
					status="营业中";
				}else{
					status="休息中";
				}
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"修改营业状态成功:"+shopId);

				this.logService.addShopAuditLog(SHOP_MY_INFO_MANAGER, "手动修改营业状态成功，修改后营业状态为："+status+"，修改人:"+this.getSessionUser().getUserName());//记录操作日志
				System.out.println("jsonObject = " + jsonObject);
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"修改营业状态失败");
			}
		} catch (BusinessException e) {
			System.out.println(e);
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}


		return this.writeJson(jsonObject);

	}
	
	
	/**
	 * 查询商家相册相片
	 * @return
	 */
	public String queryShopPhoto(){
		JSONObject jsonObject=new JSONObject();
		
		String shopId = this.getSessionUser().getShopId();
		
		List<ErpShopImage> erpShopImageList=this.shopService.queryShopPhotoList(shopId);
		
		JSONArray jsonArray = new JSONArray();
		
		HashMap<String, Object> hashMap;
		
		for(int i=0;i<erpShopImageList.size();i++){

			hashMap=new HashMap<String, Object>();
			
			hashMap.put(ErpShopImage.COL_IMAGE_ID, erpShopImageList.get(i).getImageId());

			hashMap.put(ErpShopImage.COL_SORT, erpShopImageList.get(i).getSort());

			hashMap.put(ErpShopImage.COL_IMAGE_URL, erpShopImageList.get(i).getImageUrl());

			jsonArray.add(hashMap);

			
		}
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		jsonObject.put("erpShopImageList", jsonArray);
		
		return this.writeJson(jsonObject);
	}
	
	
	
//	/**
//	 * 添加商家相册
//	 * @return
//	 */
//	public String insertShopPhoto(){
//		 JSONObject jsonObject = new JSONObject();
//			try{
//			String shopId = this.getSessionUser().getShopId();
//			String sort = this.request.getParameter("sort");
//			String imageUrl = this.request.getParameter("imageUrl");
//			
//			
//		    this.shopService.addShopPhotos(shopId,sort,imageUrl);
//		    jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"添加商家相片成功");
//			this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "添加商家相片成功，相片位置:"+sort);//记录操作日志
//		    }catch(BusinessException e){
//		    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
//			}catch (Exception e) {
//				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
//			}
//			return this.writeJson(jsonObject);
//	}
	
	
	/**
	 * 更新商家相册
	 * @return
	 */
	public String updateShopPhoto(){
		 JSONObject jsonObject = new JSONObject();
			try{
				String shopId = this.getSessionUser().getShopId();
				String sort = this.request.getParameter("sort");
				String imageUrl = this.request.getParameter("imageUrl");
				
				if(this.shopService.exitsPhotoBysort(shopId,sort)){//判断该位置是否存在相片，若存在，则更新，若不存在，则插入
					
					String imageId = this.request.getParameter("imageId");
					int ret=this.shopService.updateShopPhotos(shopId,imageId,sort,imageUrl);
					if(ret>0){
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
						jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新商家相片成功");
						this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "更新商家相片成功，相片位置:"+sort);//记录操作日志
					}else{
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
						jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新商家相片失败");
					}
				}else{
					this.shopService.addShopPhotos(shopId,sort,imageUrl);
					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
					jsonObject.put(ErrorMsg.KEY_MESSAGE,"添加商家相片成功");
					this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "添加商家相片成功，相片位置:"+sort);//记录操作日志
				}
		    }catch(BusinessException e){
		    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
			}catch (Exception e) {
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
			}
			return this.writeJson(jsonObject);
	}
	
	/**
	 * 删除相册照片
	 * @return
	 */
	public String deleteShopPhoto(){
		JSONObject jsonObject = new JSONObject();
		try{
			String sort = this.request.getParameter("sort");
			String imageId = this.request.getParameter("imageId");

			int ret=this.shopService.deleteShopPhotos(imageId);
			if(ret>0){
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"删除商家相片成功");
				this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "删除商家相片成功，删除位置:"+sort);//记录操作日志
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"删除商家相片失败");
			}
	    }catch(BusinessException e){
	    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 保存二级域名
	 * @return
	 */
	public String saveDomain(){
		JSONObject jsonObject = new JSONObject();
		try{
			String shopId = this.getSessionUser().getShopId();
			String secondDomain = this.request.getParameter("secondDomain");
			//查询商家二级域名设置的次数
			int nums = this.shopService.setDomainNums(shopId);
			if(nums<3){
				if(this.shopService.isReg(shopId, secondDomain)){
					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
					jsonObject.put(ErrorMsg.KEY_MESSAGE,"此域名已存在");
				}else{
					int ret = this.shopService.updateDomain(shopId,secondDomain);
					if(ret>0){
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
						jsonObject.put(ErrorMsg.KEY_MESSAGE,"二级域名设置成功");
						this.logService.addShopAuditLog(SHOP_STAFF_MANAGER, "商家编码:"+shopId+"二级域名设置成功");//记录操作日志
					}else{
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
						jsonObject.put(ErrorMsg.KEY_MESSAGE,"二级域名设置失败");
					}
				}
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"您的二级域名最多只能设置3次");
			}
		}catch(BusinessException e){
	    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 检查域名是否注册
	 * @return
	 */
	public String checkIsReg(){
		JSONObject jsonObject = new JSONObject();
		try{
			String secondDomain = this.request.getParameter("secondDomain");
			String shopId = this.getSessionUser().getShopId();
			Boolean ret = this.shopService.isReg(shopId,secondDomain);
			if(ret){
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"此域名已注册");
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"此域名可以使用");
			}
		}catch(BusinessException e){
	    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 获取个性域名
	 * @return
	 */
	public String getDomainByShopId(){
		JSONObject jsonObject = new JSONObject();
		try{
			String shopId = this.getSessionUser().getShopId();
			Shop tempshop = (Shop) this.shopService.one(shopId);
			int doneNum = this.shopService.setDomainNums(shopId);//已设置过的次数
			int todoNum = 0;//剩下设置的次数
			if(tempshop!=null){
			if(doneNum<3){
				todoNum = (3-doneNum);
			}
			jsonObject.put("doneNum", doneNum);
			jsonObject.put("todoNum", todoNum);
			jsonObject.put(Shop.COL_URL, tempshop.getUrl()==null?"":tempshop.getUrl());
			}
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch(BusinessException e){
	    	jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getErrorMsg());
		}catch (Exception e) {
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,e.getMessage());
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 获取配送方式列表
	 * @return
	 */
	public String getErpDeliveryList(){
		JSONObject jsonObject=new JSONObject();

		   
		   try {
				List<ErpDelivery> erpDeliveryList = this.erpDeliveryService.list(null);//获取配送方式列表
				
				
					JSONArray jsonArray = new JSONArray();
					
					HashMap<String, Object> hashMap=new HashMap<String, Object>();
					
					for(int i=0;i<erpDeliveryList.size();i++){
						
						if("1".equals(erpDeliveryList.get(i).getDeliveryId())){
							continue;
						}
					
						hashMap.put(ErpDelivery.COL_DELIVERY_ID, erpDeliveryList.get(i).getDeliveryId());
						
						hashMap.put(ErpDelivery.COL_DELIVERY_NAME, erpDeliveryList.get(i).getDeliveryName());
						
						
						jsonArray.add(hashMap);
						
					}
					
					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
					
					jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

					
					jsonObject.put("erpDeliveryListData", jsonArray);
					
					
					
			} catch (Exception e) {
				e.printStackTrace();
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
			}
		   
		   return this.writeJson(jsonObject);
	}
}